package rabbitMqLearn.spring_boot.publish;

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import rabbitMqLearn.mq.config.Config;
import rabbitMqLearn.spring_boot.confg.SpringConfig;

import java.util.Random;

@RestController
@RequestMapping("publish")
public class PublishClient {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequestMapping("fanout")
    String fanout(@RequestParam("msg") String msg) {
        System.out.println("收到 msg: " + msg);
        rabbitTemplate.convertAndSend(SpringConfig.PUBLISH_FANOUT_EXCHANGE, "", msg);
        return msg;
    }

    @RequestMapping("routing")
    String routing(@RequestParam("msg") String msg) {
        System.out.println("收到 msg: " + msg);
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend(SpringConfig.PUBLISH_ROUTING_EXCHANGE,
                    i % 2 == 0 ? SpringConfig.PUBLISH_ROUTING_KEY_1 : SpringConfig.PUBLISH_ROUTING_KEY_2, msg + i);
        }
        return msg;
    }

    @RequestMapping("topic")
    String topic(@RequestParam("msg") String msg) {
        System.out.println("收到 msg: " + msg);
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend(SpringConfig.TOPIC_EXCHANGE,
                    i % 2 == 0 ? "wwww.error" : "xxx.info", msg + i);
        }
        return msg;
    }

    //确认应答
}
